package com.test.iran;
import com.vividsolutions.jts.io.ParseException;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import sim.display.Console;
import sim.display.Controller;
import sim.display.Display2D;
import sim.display.GUIState;
import sim.engine.SimState;
import sim.portrayal.geo.GeomVectorFieldPortrayal;
import sim.portrayal.geo.GeomPortrayal;

public class IranTestUI extends GUIState{
	private Display2D display;
    private JFrame displayFrame;

    private GeomVectorFieldPortrayal roadsPortrayal = new GeomVectorFieldPortrayal(true);
    private GeomVectorFieldPortrayal districtsPortrayal = new GeomVectorFieldPortrayal(true);
    private GeomVectorFieldPortrayal agentPortrayal = new GeomVectorFieldPortrayal();

    public IranTestUI(SimState state){
        super(state);
    }

    public IranTestUI() throws ParseException{
        super(new IranTest(System.currentTimeMillis()));
    }
    
    public void quit(){
		super.quit();
		if(displayFrame!=null) displayFrame.dispose();
		displayFrame = null;
		display = null;
	}

    public void init(Controller controller){
        super.init(controller);

        display = new Display2D(IranTest.WIDTH, IranTest.HEIGHT, this);
        display.attach(districtsPortrayal, "Political Districts");
        display.attach(roadsPortrayal, "Road Network");
        display.attach(agentPortrayal, "People");

        displayFrame = display.createFrame();
        controller.registerFrame(displayFrame);
        displayFrame.setVisible(true);
    }


    public void start(){
        super.start();
        setupPortrayals();
    }

    private void setupPortrayals(){
        IranTest world = (IranTest)state;

        districtsPortrayal.setField(world.districts);
        districtsPortrayal.setPortrayalForAll(new GeomPortrayal(Color.LIGHT_GRAY, false));
        
        roadsPortrayal.setField(world.roads);
        roadsPortrayal.setPortrayalForAll(new GeomPortrayal(Color.pink,true));

        agentPortrayal.setField(world.agents);
        agentPortrayal.setPortrayalForAll(new GeomPortrayal(Color.BLACK,0.03, true));
        
        display.reset();
        display.setBackdrop(Color.WHITE);
        display.repaint();
    }

    public static void main(String[] args){
        IranTestUI worldGUI = null;

        try{
            worldGUI = new IranTestUI();
        }
        catch (ParseException ex){
            Logger.getLogger(IranTestUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        Console console = new Console(worldGUI);
        console.setVisible(true);
    }
}